﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnManager : MonoBehaviour
{
    public GameObject enemyPrefab;
    public float spawnRage;
    public GameObject powerUpPrefab;

    public int enemyCount;
    public int waveNumber = 1;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        enemyCount = FindObjectsOfType<Enemy>().Length;

        if(enemyCount==0)
        {
            SpawnEnemy(waveNumber++);
            Instantiate(powerUpPrefab, RandomPos(), powerUpPrefab.transform.rotation);
        }
    }


    public Vector3 RandomPos()
    {
        float spawnPosX = Random.Range(-spawnRage, spawnRage);
        float spawnPosZ = Random.Range(-spawnRage, spawnRage);

        return new Vector3(spawnPosX, 0, spawnPosZ);
    }

    void SpawnEnemy(int wave)
    {
        for (int i = 0; i < wave; i++)
        {
            RandomPos();
            Instantiate(enemyPrefab, RandomPos(), Quaternion.identity);
        }
    }
}
